<?php
header ( 'Content-Type:text/html;charset=gbk' );
require_once ("db_manage.php");
$routeid = $_POST ['routeid'];
if ($routeid == "") {
	$routeid = $_GET ['routeid'];
}
$title = $_POST ['title'];
$area = $_POST ['area'];
$type = $_POST ['type'];
$expense = $_POST ['expense'];
$num = $_POST ['num'];
$view = $_POST ['view'];
$bed = $_POST ['bed'];
$traffic = $_POST ['traffic'];
$edit = $_POST ['edit'];

$costdate = $_POST ['costdate'];
$backdate = $_POST ['backdate'];

$routeBox = $_POST ['routeBox'];
$back = $_GET ['back'];
$travelid = $_POST ['travelid'];
$days = $_POST ['days'];
$traveldays = $_POST ['traveldays'];
$address = $_POST ['address'];

$postuser = $_POST ['postuser'];

$content = $_POST ['content'];
$equipment = $_POST ['equipment'];
$switch = $_POST ['switch'];

if ($edit == "edit_route") {
	if ($routeid != "") {
		$sql = "update pcd_route set title='" . $title . "',area='" . $area . "',type='" . $type . "',expense='" . $expense . "',num='" . $num . "',traffic='" . $traffic . "',hot=0,view='" . $view . "',bed='" . $bed . "',timestamp=now(),state=1, content = '" . $content . "', switch = '" . $switch . "', equipment = '" . $equipment . "',postuser = '" . $postuser . "' where rid=" . $routeid;
	}
	mysql_query ( $sql );
	
	$sql = "delete from pcd_travel_route where routeid = " . $routeid;
	mysql_query ( $sql );
	
	$len = sizeof ( $routeBox );
	
	for($i = 0; $i < $len; $i ++) {
		//		echo "<br>".$routeBox[$i];
		if ($routeBox [$i] != "") {
			$array = explode ( "_", $travelid [$i] );
			$temptravelid = $array [0];
			$temptype = $array [1];
			if ($temptype != 'scence') {
				$temptype = 'travel';
			}
			$sql = "insert into pcd_travel_route(routeid,travelid,type,travelname,area,postuser,timestamp) values(" . $routeid . "," . $temptravelid . ",'" . $temptype . "','" . $routeBox [$i] . "','" . $area . "'," . $postuser . ",now())";
			//			echo "<br>".$sql;
			mysql_query ( $sql );
		}
	}
	
	echo "<script>location.href='route_detail.php?id=" . $routeid . "';</script>";

} else if ($edit == "add_route") {
	if ($routeid == "") {
		$sql = "insert into pcd_route(title,area,type,expense,num,days,traffic,hot,view,bed,timestamp,state,content,switch,equipment,postuser,mark) values('" . $title . "','" . $area . "','" . $type . "','" . $expense . "','" . $num . "'," . $traveldays . ",'" . $traffic . "',0,'" . $view . "','" . $bed . "',now(),0,'" . $content . "','" . $switch . "','" . $equipment . "'," . $postuser . ",0)";
	} else {
		$sql = "update pcd_route set title='" . $title . "',area='" . $area . "',type='" . $type . "',expense='" . $expense . "',num='" . $num . "',traffic='" . $traffic . "',hot=0,view='" . $view . "',bed='" . $bed . "',timestamp=now(),state=0, content = '" . $content . "', switch = '" . $switch . "', equipment = '" . $equipment . "',postuser = '" . $postuser . "' where rid=" . $routeid;
	}
	
	//echo "<br>sql:".$sql;	
	

	mysql_query ( $sql );
	$result_id = getLastId ();
	while ( ($rs = mysql_fetch_object ( $result_id )) != false ) {
		$lastid = $rs->id;
	}
	if ($routeid == "") {
		$routeid = $lastid;
	}
	
	//echo "<br>routeid:".$routeid;
	

	$len = sizeof ( $routeBox );
	
	for($i = 0; $i < $len; $i ++) {
		
		if ($routeBox [$i] != "") {
			$array = explode ( "_", $travelid [$i] );
			$temptravelid = $array [0];
			$temptype = $array [1];
			
			if ($temptype != 'scence') {
				$temptype = 'travel';
			}
			$sql = "insert into pcd_travel_route(routeid,travelid,type,travelname,area,postuser,timestamp) values(" . $lastid . "," . $temptravelid . ",'" . $temptype . "','" . $routeBox [$i] . "','" . $area . "'," . $postuser . ",now())";
			mysql_query ( $sql );
		}
	}
	echo "<script>location.href='route_post.php?routeid=" . $routeid . "&title=" . $title . "&content=" . $content . "';</script>";
}

if ($edit == "add_content") {
	$sql = "update pcd_route set content = '" . $content . ",switch = '" . $switch . "', equipment = '" . $equipment . "' where routeid = " . $routeid;
	mysql_query ( $sql );
	echo "<script>location.href='route_post.php?routeid=" . $routeid . "&title=" . $title . "';</script>";
}

?>
